package com.bobo.web.api.vo.base;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * Author: v
 * Date: 2018/10/19
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class    ResponseResult<T> extends AbstractModel {

    private int code;
    private String message;
    private T data;

    public ResponseResult() {
        this.code = ReturnCode.SUCCESS.getCode();
        this.message = ReturnCode.SUCCESS.getMsg();
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    //******************************** Result method *********************************//

    public static final ResponseResult<String> success() {
        return success(null);
    }

    public boolean isSuccess() {
        if(code == ReturnCode.SUCCESS.getCode()){
            return true;
        }
        return false;
    }

    public static final <T> ResponseResult<T> success(T t) {
        ResponseResult result = new ResponseResult();
        result.setCode(ReturnCode.SUCCESS.getCode());
        result.setMessage(ReturnCode.SUCCESS.getMsg());
        result.setData(t);
        return result;
    }

    public static final <T> ResponseResult<T> error(String message) {
        return error(ReturnCode.ERROR.getCode(), message);
    }

    public static final <T> ResponseResult<T> error(Integer code, String message) {
        ResponseResult result = new ResponseResult();
        result.setCode(code.intValue());
        result.setMessage(message);
        return result;
    }
}
